package com.xqboss.web.controller.punch;

import com.github.pagehelper.Page;
import com.xqboss.apps.convert.punch.PunchRuleConverter;
import com.xqboss.apps.domain.punch.PunchRule;
import com.xqboss.apps.dto.PageDto;
import com.xqboss.apps.dto.punch.AddPunchRuleDto;
import com.xqboss.apps.dto.IdDto;
import com.xqboss.apps.dto.punch.ChangePunchSaleableDto;
import com.xqboss.apps.dto.punch.GetPunchRuleListDto;
import com.xqboss.apps.vo.punch.PunchRuleVo;
import com.xqboss.apps.service.punch.PunchRuleService;
import com.xqboss.common.annotation.Log;
import com.xqboss.common.core.controller.BaseController;
import com.xqboss.common.core.domain.R;
import com.xqboss.common.core.page.PageDomain;
import com.xqboss.common.core.page.TableDataInfo;
import com.xqboss.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Objects;

/**
 * @author Trent
 * @date 2023/8/15
 */
@Api(tags = {"打卡-打卡管理"})
@RestController("WebPunchRuleController")
@RequestMapping("/web/punch/rule")
public class WebPunchRuleController extends BaseController {

    @Autowired
    private PunchRuleService punchRuleService;

    @Autowired
    private PunchRuleConverter punchRuleConverter;

    @PreAuthorize("@ss.hasPermi('web:punch:rule:add')")
    @ApiOperation("添加打卡规则")
    @PostMapping("add")
    @Log(title = "打卡规则", businessType = BusinessType.INSERT)
    public R addRule(@Validated @RequestBody AddPunchRuleDto params) {
        punchRuleService.addRule(params);
        return R.ok();
    }

    @PreAuthorize("@ss.hasPermi('web:punch:rule:edit')")
    @ApiOperation("修改打卡规则")
    @PutMapping("edit/{id}")
    @Log(title = "打卡规则", businessType = BusinessType.UPDATE)
    public R edit(
            @PathVariable("id")
            @ApiParam(value = "打卡规则ID", required = true)
            @NotNull(message = "请输入打卡规则ID")
            Long id,
            @Validated  @RequestBody AddPunchRuleDto params) {
        punchRuleService.editRule(id, params);
        return R.ok();
    }

    @PreAuthorize("@ss.hasPermi('web:punch:rule:change-saleable')")
    @ApiOperation("打卡规则上下架操作")
    @PutMapping("change-saleable")
    @Log(title = "打卡规则", businessType = BusinessType.UPDATE)
    public R changeSaleable(@Validated @RequestBody ChangePunchSaleableDto params) {
        punchRuleService.changeSaleable(params.getId(), params.getSaleable());
        return R.ok();
    }

    @PreAuthorize("@ss.hasPermi('web:punch:rule:del')")
    @ApiOperation("删除打卡规则")
    @DeleteMapping("del/{id}")
    @Log(title = "打卡规则", businessType = BusinessType.DELETE)
    public R del(@PathVariable("id")
                 @ApiParam(value = "打卡规则ID", required = true)
                 @NotNull(message = "请输入打卡规则ID")
                 Long id) {
        punchRuleService.removeById(id);
        return R.ok();
    }

    @PreAuthorize("@ss.hasPermi('web:punch:rule:get')")
    @ApiOperation("获取打卡规则")
    @GetMapping("get/{id}")
    public R<PunchRuleVo> getRule(@PathVariable("id")
                                  @ApiParam(value = "打卡规则ID", required = true)
                                  @NotNull(message = "请输入打卡规则ID")
                                  Long id) {
        PunchRule result = punchRuleService.getById(id);
        Objects.requireNonNull(result, "资源不存在");
        PunchRuleVo vo = punchRuleConverter.toVo(result);
        return R.ok(vo);
    }

    @PreAuthorize("@ss.hasPermi('web:punch:rule:list')")
    @ApiOperation("获取打卡规则列表")
    @GetMapping("list")
    public R<List<PunchRuleVo>> getRuleList(GetPunchRuleListDto pageInfo) {
        if(Objects.isNull(pageInfo.getOrderByColumn())){
            pageInfo.setOrderByColumn("id");
            pageInfo.setSortAsc(false);
        }
        pageInfo.startPageAndOrder();
        List<PunchRule> list = punchRuleService.getList(pageInfo.getSaleable(), pageInfo.getKeyword());
        List<PunchRuleVo> vo = punchRuleConverter.toVoList(list);
        return R.ok(vo, list);
    }
}
